package cn.sms.org.controller;

import cn.sms.basic.util.AjaxResult;
import cn.sms.basic.util.PageList;
import cn.sms.org.domain.DictionaryDetail;
import cn.sms.org.query.DictionaryDetailQuery;
import cn.sms.org.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService iDictionaryDetailService;

    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return iDictionaryDetailService.queryById(id);
    }

    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return iDictionaryDetailService.queryAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iDictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }

    }

    @PutMapping
    public AjaxResult save(@RequestBody DictionaryDetail dictionaryDetail){
        System.out.println(dictionaryDetail);
        try {
            if (dictionaryDetail.getId()==null){
                iDictionaryDetailService.add(dictionaryDetail);
            }else {
                iDictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/list")
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query){
        return iDictionaryDetailService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult batchRemoveDictionaryDetail(@RequestBody List<Long> ids){
        try {
            iDictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败哟");
        }
    }

}
